package GlavFinans;

use Data::Dumper;
use Mojo::Base 'Mojolicious';
use Cache::Memcached; 
use GlavFinans::Schema;
use GlavFinans::Validator;

our $VERSION = '0.1';
our $DEBUG = ((!$ENV{MOJO_MODE} || $ENV{MOJO_MODE} =~ /^dev/x) ? 1 : 0);

has config => sub {{}};
has 'memcached';
has 'db';

sub startup {
	my $self = shift;
	
	my $cfile = sprintf('%s/etc/glavfinans.conf', $self->home);
	my $config = $self->plugin('Config', file => $cfile, default => {} );
	
	$self->config($config);
	$self->secret($config->{secret});
	
	$self->session(
		'expires' => $config->{'session'}->{'expire'},
		'name'    => $config->{'session'}->{'name'}
	);

	my $memc = $config->{'memcached'};
	$self->memcached( 
		Cache::Memcached->new({
	        'servers'            => $memc->{'servers'},
	        'debug'              => $memc->{'debug'} || 0,
	        'compress_threshold' => $memc->{'compress'} || 10_000,
    	})
    );
    
    my $captcha = $config->{'captcha'};
    $self->plugin('captcha_renderer' => { 
    	'size'           => $captcha->{'font'}->{'size'}      || 30, 
    	'color'          => $captcha->{'font'}->{'color'}     || 'blue',
    	'font'           => $captcha->{'font'}->{'name'}      || undef, 
    	'wave_amplitude' => $captcha->{'wave'}->{'amplitude'} || 7,
    	'wave_length'    => $captcha->{'wave'}->{'length'}    || 80
    });

	my $mail = $config->{'mail'};
	$self->plugin('mail' => {
		'from' => $mail->{'from'}, 
		'type' => $mail->{'content-type'}, 
		'how'  => $mail->{'transport'}
	});
	
	$self->initdb();
	
	my $validator = GlavFinans::Validator->new();
	
	$self->defaults(
		'db'        => $self->db,
		'config'    => $self->config,
		'layout'    => 'frontend',
		'memcached' => $self->memcached,
		'validator' => $validator
	);
	
	$self->plugin('Mojolicious::Plugin::CSRFDefender');
	$self->plugin('charset' => { 'charset' => 'utf-8' });
	
	$self->hook(after_dispatch => sub {
		my $c = shift;
		my $session = $c->stash->{'session'};
		if ( $session ) {
			$session->flush();
		};
	});
	
	for my $name (qw/req/) {
    	$self->helper($name => sub { shift->$name(@_) });
  	}

	$self->plugin('GlavFinans::Module::Core');
}

sub initdb {
	my $self = shift; 
	my $dbc = $self->config->{database};
	my $dsn = sprintf ('DBI:Pg:dbname=%s;host=%s;port=%d', $dbc->{name}, $dbc->{host}, $dbc->{port});
	
	my $db = GlavFinans::Schema->connect( $dsn, $dbc->{'user'}, $dbc->{'pass'}, $dbc->{attr}) || die $!;
	if ( $dbc->{trace} ) {
		$db->storage->debugfh(IO::File->new($self->home.$dbc->{trace}, 'a'));
		$db->storage->debug(100);
	}
	
	$self->db($db);
}

sub route {
	my $self  = shift;
	my $route = shift;
	
	foreach my $part ( %{$route} ) 
	{
		my $data = $route->{$part};
		my $layout = $data->{layout};
		my $bridge = $data->{bridge};
		my $namespace = $data->{namespace};
		my $list = $data->{route};
		
		for my $item ( @$list ) 
		{
			my $method      = $item->[0];
			my $match       = $item->[1];
			my $controller  = $item->[2];
			my $action      = $item->[3];
			my $name        = $item->[4];
			 
			$bridge->$method($match)
				->to(namespace => $namespace,
					controller => $controller,
					action     => $action,
					layout     => $layout)
				->name($name);
		}
	}
}
1;
